package TransportesMarroquin;

import java.awt.*; 
import java.awt.Component;
import javax.media.*;
import javax.media.control.*;
import javax.media.util.*;
import javax.media.format.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.RenderedImage;
import javax.swing.JOptionPane;


class CamaraWeb{
	public Component componente;
	public Player p;
	public Component video;
        static String direc;
	public MediaLocator ml=new MediaLocator("vfw://0");
        public String j=null,h=null;
        NuevoPiloto nue = new NuevoPiloto();
	CamaraWeb(){
		Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
		try{
			ml = new MediaLocator("vfw://0");
			p = Manager.createRealizedPlayer(ml);
			video = p.getVisualComponent();
			p.start();
	    	if ( video != null ){
	               componente=video;
			}
		}catch(Exception e){
			//JOptionPane.showMessageDialog(null,"Error de Comunicacion con la WebCam " + e);
		}
	}
	public Component VerCamara(int x,int y,int alto,int ancho){
		this.componente.setBounds(x, y, alto, ancho);
                return componente;
                
	}
	
	public int Fotografiar(String Carpeta,Boolean NombreAutomatico,String NombreValoNull){
		Buffer buf=null;
		Image img=null;
		File imagenArch;
		String nombre=null;
		String formato = null;
		File CarpetaFotografias;
		
		CarpetaFotografias = new File(Carpeta);
		if(CarpetaFotografias.exists()==false){
			CarpetaFotografias.mkdir();
		}
		FrameGrabbingControl fgc = (FrameGrabbingControl)
    	p.getControl("javax.media.control.FrameGrabbingControl");
    	buf = fgc.grabFrame();
    	BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat());
    	img = btoi.createImage(buf);
    	if (img != null){
                j = JOptionPane.showInputDialog(null,"Ingresar Nombre y Apellido.");
    			nombre=Carpeta+ "\\"+j+".jpg";
    			
     			imagenArch = new File(nombre);
         		formato = "JPEG";
				try{
					ImageIO.write((RenderedImage) img,formato,imagenArch);
                                        direc = ""+System.getProperty("user.dir")+"\\Piloto\\"+j+".jpg";
					return 1;
         		}catch (IOException ioe){
         			return 0;
         		}
         }
         //NuevoPiloto.cfoto.setText();
        video.disable();
        return 0;
	}

        

}